<?php
namespace app\admin\controller;
use app\admin\model\AuthGroup as AuthGroupModel;
use app\admin\model\AuthRule as AuthRuleModel;
use app\admin\controller\Base;
use think\Db;
use app\admin\controller\Common;

class AuthGroup extends Base
{
    public function lst(){
        $list=AuthGroupModel::paginate(20);
        $this->assign('list',$list);
        $authRule=new \app\admin\model\AuthRule();
        $authRuleRes=$authRule->authRuleTree();
        $this->assign('authRuleRes',$authRuleRes);
        return view();
    }

    public function add(){
        if(request()->isPost()) {

            $data = input('post.');
            if($data['rules']){
                $data['rules']=implode(',', $data['rules']);
            }
            $add = db('auth_group')->insert($data);
            if ($add) {
                $this->success('添加用户组成功', url('lst'));
            } else {
                $this->success('添加用户组失败', url('lst'));
            }
            return;


        }
        $authRule = new \app\admin\model\AuthRule();
        $authRuleRes = $authRule->authRuleTree();
        $this->assign('authRuleRes', $authRuleRes);
        return view();
    }

    public function edit(){
        if(request()->isPost()){
            $data=input('post.');
            if($data['rules']){
                $data['rules']=implode(',', $data['rules']);
            }
            $_data=array();
            foreach ($data as $k => $v) {
                $_data[]=$k;
            }
            if(!in_array('status', $_data)){
                $data['status']=1;
            }
            $save=db('auth_group')->update($data);
            if($save!==false){
                $this->success('修改用户组成功！',url('lst'));
            }else{
                $this->error('修改用户组失败！');
            }
            return;
        }
        $authgroups=db('auth_group')->find(input('id'));
        $this->assign('authgroups',$authgroups);
        $authRule=new \app\admin\model\AuthRule();
        $authRuleRes=$authRule->authRuleTree();
        $this->assign('authRuleRes',$authRuleRes);
        return view();
    }



    public function del()
    {
        $id=input('id');

            if (db('auth_group')->where('id','=',$id)->delete()){
                $this->success('用户组删除成功','lst');
            }else {
                $this->error('用户组删除失败','lst');
            }


    }


    //Ajax异步修改管理员状态
    public function changestatus(){
        if(request()->isAjax()){
            $userid=input('userid');
            $status=db('auth_group')->field('status')->where('id',$userid)->find();
            $status=$status['status'];
            if($status==1){
                db('auth_group')->where('id',$userid)->update(['status'=>0]);
                echo 1;//由显示改为隐藏
            }
            elseif($status==0){
                db('auth_group')->where('id',$userid)->update(['status'=>1]);
                echo 2;//由隐藏改为显示
            }
        }
        else{
            $this->error("非法操作");
        }

    }

}
